﻿using AutoFlatnessTest.Plc;
using AutoFlatnessTest.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// UcSetStock.xaml 的交互逻辑
    /// </summary>
    public partial class UcSetInfo : UserControl
    {
        UcSetInfoVModel vModel = new UcSetInfoVModel();

        int addval = 10;
        int minval = 1;

        public UcSetInfo()
        {
            InitializeComponent();

            this.DataContext = vModel;
        }

        private void gridLoad(object sender, RoutedEventArgs e)
        {
            if (!(sender is Grid grid))
                return;

            var rowCount = grid.RowDefinitions.Count;
            var columnCount = grid.ColumnDefinitions.Count;
            var thickness = new Thickness(1);
            var bottomThickness = new Thickness(0, 0, 0, 1);
            var rightThickness = new Thickness(0, 0, 1, 0);
            //var headerBack = new SolidColorBrush(Color.FromArgb(255, 129, 133, 145));
            var borderBrush = Brushes.LightGray;
            for (int i = 0; i < rowCount; i++)
            {
                Border border = new Border()
                {
                    BorderBrush = borderBrush,
                    BorderThickness = i == 0 ? thickness : bottomThickness,
                    //Background = i == 0 ? headerBack : Brushes.Transparent,
                };
                //border.SetValue(Panel.ZIndexProperty, -1000);
                border.SetValue(Grid.RowProperty, i);
                border.SetValue(Grid.ColumnProperty, 0);
                border.SetValue(Grid.ColumnSpanProperty, columnCount);
                grid.Children.Add(border);
            }
            for (int i = 0; i < columnCount; i++)
            {
                Border border = new Border()
                {
                    BorderBrush = borderBrush,
                    BorderThickness = i == 0 ? thickness : rightThickness,
                    //Background = Brushes.Transparent,
                };
                //border.SetValue(Panel.ZIndexProperty, -1000);
                border.SetValue(Grid.RowProperty, 0);
                border.SetValue(Grid.ColumnProperty, i);
                border.SetValue(Grid.RowSpanProperty, rowCount);
                grid.Children.Add(border);
            }
            grid.Loaded -= gridLoad;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (MainWindow.SelectItemNum != 51)
                return;

            Task.Run(() =>
            {
                vModel.Vis = Visibility.Visible;

                while (true)
                {
                    if (MainWindow.SelectItemNum != 51)
                        break;

                    try
                    {
                        vModel.VisLjck = Visibility.Hidden;

                        Thread.Sleep(300);

                        vModel.MoShi = InovanceRun.Plc.GetData<Int16>(InovanceComm.模式) == 1 ? "手动" : "自动";

                        vModel.Yz = $"手动：{InovanceRun.Plc.GetData<Int32>(InovanceComm.运载手动速度)} 自动：{InovanceRun.Plc.GetData<Int32>(InovanceComm.运载自动速度)}";
                        vModel.Smi = $"手动：{InovanceRun.Plc.GetData<Int32>(InovanceComm.扫描仪手动速度)} 自动：{InovanceRun.Plc.GetData<Int32>(InovanceComm.扫描仪自动速度)}";
                        vModel.Sma = $"手动：{InovanceRun.Plc.GetData<Int32>(InovanceComm.扫码枪手动速度)} 自动：{InovanceRun.Plc.GetData<Int32>(InovanceComm.扫码枪自动速度)}";

                        Thread.Sleep(800);
                    }
                    catch (Exception ex)
                    {
                        vModel.SetLjsb();
                    }
                    finally
                    {
                        vModel.Vis = Visibility.Collapsed;
                    }

                    if (vModel.VisLjck == Visibility.Visible)
                        break;

                }
            });
        }

        private void shuxin(object sender, RoutedEventArgs e)
        {
            UserControl_Loaded(null, null);
        }

        private void yzzj(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.运载自动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.运载自动速度, sd + addval);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void yzjs(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.运载自动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.运载自动速度, (sd - addval) <= minval ? minval : (sd - addval));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMizj(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫描仪自动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫描仪自动速度, sd + addval);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMijs(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫描仪自动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫描仪自动速度, (sd - addval) <= minval ? minval : (sd - addval));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMajs(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫码枪自动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫码枪自动速度, (sd - addval) <= minval ? minval : (sd - addval));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMajz(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫码枪自动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫码枪自动速度, sd + addval);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void yzzjs(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.运载手动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.运载手动速度, sd + addval);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void yzjss(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.运载手动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.运载手动速度, (sd - addval) <= minval ? minval : (sd - addval));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMizjs(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫描仪手动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫描仪手动速度, sd + addval);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMijss(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫描仪手动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫描仪手动速度, (sd - addval) <= minval ? minval : (sd - addval));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMajzs(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫码枪手动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫码枪手动速度, sd + addval);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }

        private void sMajss(object sender, RoutedEventArgs e)
        {
            try
            {
                var sd = InovanceRun.Plc.GetData<Int32>(InovanceComm.扫码枪手动速度);
                InovanceRun.Plc.SetData<Int32>(InovanceComm.扫码枪手动速度, (sd - addval) <= minval ? minval : (sd - addval));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"失败：{ex.Message}");
            }
        }
    }
}
